package org.trc.trajectory;

import java.util.ArrayList;
import java.util.GregorianCalendar;


public class Trajectory {
	public String carID;
	GregorianCalendar startTime;
	GregorianCalendar endTime;
	
	public ArrayList <TrajectoryPoint> points = new ArrayList <TrajectoryPoint> ();
	
	public Trajectory(String carID){
		this.carID = carID;
	}
	
	public Trajectory(String cid, GregorianCalendar startTime, GregorianCalendar endTime){
		this.carID = cid;
		this.startTime = startTime;
		this.endTime = endTime;
	}
    
	public void addTrajectoryPoint(TrajectoryPoint point){
		this.points.add(point);
	}
	
	public GregorianCalendar getStartTime(){
		if(startTime == null)
			startTime = points.get(0).timestamp;
		return startTime;
	}
	
	public GregorianCalendar getEndTime(){
		if(endTime == null)
			endTime = points.get(points.size() - 1).timestamp;
		return endTime;
	}

	public void showHistory(){
		System.out.println("Car:" + carID );
		for(int i = 0; i < points.size(); i++){
			System.out.println("#:" + i + " " + points.get(i).seconds +  ","  +  points.get(i).linkID + "," + points.get(i).tripID);
		}
	}

    public Trip[] getTrip(){
    	ArrayList <Trip> tripList = new ArrayList <Trip> ();
    	
    	Trip trip = null;
    	String curTripID = null;
    	String preTripID = null;
    	
    	for(TrajectoryPoint point : points){
    		curTripID = point.tripID; 
    		if(curTripID != null && !curTripID.equals(preTripID)) {
    			trip = new Trip(carID, point.tripID);
    			tripList.add(trip);
    			trip.addTrajectoryPoint(point);
    		} else if(curTripID != null && curTripID.equals(preTripID)){
    			trip.addTrajectoryPoint(point);
    		}
    		preTripID = curTripID;
    	}

    	return tripList.toArray(new Trip[tripList.size()]);	
    }

}
